package com.aitaoxue.palx.fragment;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.api.remote.AitaoXueApi;
import com.aitaoxue.palx.base.BaseFragment;
import com.aitaoxue.palx.db.GoodDBHelper;
import com.aitaoxue.palx.ui.register.GuideActivity;
import com.aitaoxue.palx.ui.register.GuideChooseActivity;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class GuideFragment_1 extends BaseFragment {

	@InjectView(R.id.layout_1)
	RelativeLayout layout1;
	@InjectView(R.id.layout_2)
	RelativeLayout layout2;
	@InjectView(R.id.school_content)
	TextView school;
	@InjectView(R.id.course_content)
	TextView course;
	@InjectView(R.id.exp_content)
	EditText exp;
	@InjectView(R.id.address_content)
	EditText address;
	@InjectView(R.id.btn_push)
	Button push;

	private static final int REQUEST_SCHOOL = 1;
	private static final int REQUEST_COURSE = 2;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_guide_1, container,
				false);

		ButterKnife.inject(this, view);
		initData();

		return view;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layout_1:
			Intent intent = new Intent(getActivity(), GuideChooseActivity.class);
			intent.putExtra("choose", "school");
			startActivityForResult(intent, 1);
			break;
		case R.id.layout_2:
			Intent intent2 = new Intent(getActivity(),
					GuideChooseActivity.class);
			intent2.putExtra("choose", "course");
			startActivityForResult(intent2, 2);
			break;
		case R.id.btn_push:
			if (checkIfReady()) {
				((GuideActivity) getActivity()).showWaitDialog();
				String res = GoodDBHelper.getKindIdFromName(course.getText()
						.toString());

				AitaoXueApi.prodRegister3(school.getText().toString(), exp
						.getText().toString(), res, address.getText()
						.toString(), mHandler);
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void initView(View view) {
	}

	@Override
	public void initData() {
		layout1.setOnClickListener(this);
		layout2.setOnClickListener(this);
		push.setOnClickListener(this);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == getActivity().RESULT_OK) {
			String res = data.getStringExtra("res");
			switch (requestCode) {
			case REQUEST_SCHOOL:
				school.setText(res);
				break;
			case REQUEST_COURSE:
				course.setText(res);
				break;
			default:
				break;
			}
		}
	}

	private boolean checkIfReady() {
		if (TextUtils.isEmpty(school.getText())) {
			((GuideActivity) getActivity()).showToast("请选择城市", 0, 0);
			return false;
		}
		if (TextUtils.isEmpty(course.getText())) {
			((GuideActivity) getActivity()).showToast("请选择擅长科目", 0, 0);
			return false;
		}
		if (TextUtils.isEmpty(exp.getText())) {
			((GuideActivity) getActivity()).showToast("请填写家教经验", 0, 0);
			return false;
		}
		if (!legalNumber(exp.getText().toString())) {
			((GuideActivity) getActivity()).showToast("家教经验在0-48之间", 0, 0);
			return false;
		}
		if (TextUtils.isEmpty(address.getText())) {
			((GuideActivity) getActivity()).showToast("请填写授课区域", 0, 0);
			return false;
		}

		return true;
	}

	private AsyncHttpResponseHandler mHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
			((GuideActivity) getActivity()).hideWaitDialog();
			String res = new String(arg2);

			try {
				JSONObject json = new JSONObject(res);
				String code = json.getString("code");
				if (code.equals("0000")) {
					((GuideActivity) getActivity()).goToNextFragment();
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

		}

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {

		}
	};

	private boolean legalNumber(String text) {
		try {
			int number = Integer.parseInt(text);
			if (number < 0 || number > 48) {
				return false;
			}
		} catch (Exception e) {
			return false;
		}

		return true;
	}

}
